﻿using DI.WebApi.Service;
using Microsoft.AspNetCore.Mvc;


namespace DI.WebApi.Controllers;


[ApiController]
[Route("[controller]/[action]")]
public class HelloController : ControllerBase {
  IHelloService _hello;

  [FromServices]
  public IHelloService MyHello { get; set; }

  public HelloController (IHelloService hello) {
    _hello = hello;
  }

  // http://localhost:8088/hello/ctor
  [HttpGet]
  public IActionResult Ctor () {
    _hello.Name = "构造实例";
    return Ok($"构造注入: {_hello.Greet()}");
  }


  // http://localhost:8088/hello/prop
  [HttpGet]
  public IActionResult Prop () {
    MyHello.Name = "属性实例";
    return Ok($"属性注入: {MyHello.Greet()}");
  }

  // 参数的FromServices特性可略
  // http://localhost:8088/hello/param
  [HttpGet]
  public IActionResult Param ([FromServices] IHelloService _p) {
    _p.Name = "参数实例";
    return Ok($"参数注入: {_p.Greet()}");
  }
}
